package cn.sylinx.horm.starter;

import cn.sylinx.horm.config.OrmConfig;
import cn.sylinx.horm.core.OrmClient;
import cn.sylinx.horm.core.SqlClient;
import cn.sylinx.horm.core.TransactionSupportOrmClient;
import cn.sylinx.horm.core.datasource.NamedDataSource;
import cn.sylinx.horm.dialect.DialectFactory;
import cn.sylinx.horm.resource.parse.DefaultSqlParser;
import cn.sylinx.horm.transaction.jdbc.JdbcTransactionalConnectionProvider;

public class DefaultSqlClientInitializor implements SqlClientInitializor {

    @Override
    public SqlClient initSqlClient(NamedDataSource signleNamedDataSource, OrmConfig ormConfig) {
        OrmClient sqlClient = new TransactionSupportOrmClient();
        sqlClient.setDialect(DialectFactory.createDialect(signleNamedDataSource.getDbType()));
        sqlClient.setSqlParser(
                new DefaultSqlParser(ormConfig.isParseSqlPathDbtype() ? signleNamedDataSource.getDbType() : null));
        // 此处会将OrmClient句柄保存内存
        sqlClient.setConnectionProvider(new JdbcTransactionalConnectionProvider(signleNamedDataSource));

        return sqlClient;
    }
}
